package org.xxpay.mch.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.common.exception.ServiceException;
import org.xxpay.core.entity.PayInterfaceTypeTemplate;
import org.xxpay.mch.common.ctrl.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(Constant.MCH_CONTROLLER_ROOT_PATH + "/config/pay_interface_type/template")
public class PayInterfaceTypeTemplateController extends BaseController {

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {

        judgeRecord(); //校验越权操作
        JSONObject param = getJsonParam(request);
        PayInterfaceTypeTemplate payInterfaceTypeTemplate = getObject(param, PayInterfaceTypeTemplate.class);
        payInterfaceTypeTemplate.setBelongInfoType(MchConstant.SYSTYPE_MCH);
        payInterfaceTypeTemplate.setBelongInfoId(getUser().getId());
        int count = commonService.payInterfaceTypeTemplateService.count(payInterfaceTypeTemplate);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<PayInterfaceTypeTemplate> payInterfaceTypeTemplateList = commonService.payInterfaceTypeTemplateService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), payInterfaceTypeTemplate);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(payInterfaceTypeTemplateList, count));
    }

    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLong(param, "id");

        judgeRecord(); //校验越权操作
        PayInterfaceTypeTemplate payInterfaceTypeTemplate = commonService.payInterfaceTypeTemplateService.findById(id);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(payInterfaceTypeTemplate));
    }

    @RequestMapping("/update")
    @ResponseBody
    @MethodLog( remark = "修改支付接口类型模板" )
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayInterfaceTypeTemplate payInterfaceTypeTemplate = getObject(param, PayInterfaceTypeTemplate.class);
        payInterfaceTypeTemplate.setParamTemplate(HtmlUtils.htmlUnescape(payInterfaceTypeTemplate.getParamTemplate())); //获取转义前的真实的数据
        judgeRecord(payInterfaceTypeTemplate.getId()); //校验越权操作
        int count = commonService.payInterfaceTypeTemplateService.update(payInterfaceTypeTemplate);
        if(count >= 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/add")
    @ResponseBody
    @MethodLog( remark = "新增支付接口类型模板" )
    public ResponseEntity<?> add(HttpServletRequest request) {
        judgeRecord();  //校验越权操作
        JSONObject param = getJsonParam(request);
        PayInterfaceTypeTemplate payInterfaceTypeTemplate = getObject(param, PayInterfaceTypeTemplate.class);
        payInterfaceTypeTemplate.setParamTemplate(HtmlUtils.htmlUnescape(payInterfaceTypeTemplate.getParamTemplate())); //获取转义前的真实的数据
        payInterfaceTypeTemplate.setBelongInfoType(MchConstant.SYSTYPE_MCH);
        payInterfaceTypeTemplate.setBelongInfoId(getUser().getId());
        int count = commonService.payInterfaceTypeTemplateService.add(payInterfaceTypeTemplate);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /** 校验账号类型和获取的数据是否合法 **/
    private void judgeRecord(){

        //当前登录用户不属于私有商户
        if(this.getUser().getType() != MchConstant.MCH_TYPE_PRIVATE){
            throw new ServiceException(RetEnum.RET_MCH_PERMISSION_ERROR);
        }
    }

    /** 校验账号类型和获取的数据是否合法 **/
    private PayInterfaceTypeTemplate judgeRecord(Long recordId){

        //当前登录用户是否属于私有商户
        judgeRecord();

        //判断是否越权查看
        PayInterfaceTypeTemplate record = commonService.payInterfaceTypeTemplateService.findById(recordId);
        if(record == null || record.getBelongInfoType() != MchConstant.SYSTYPE_MCH || !record.getBelongInfoId().equals(this.getUser().getId())){
            throw new ServiceException(RetEnum.RET_MCH_PERMISSION_ERROR);
        }
        return record;
    }

}
